﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace MusicDatabase
{
    /*
     * Auteur:  Joris Molnár en Niels van Leusden
     * Datum:   31-1-2013
     * Omschrijving: Deze klasse vult het scherm AlbumInfo, met gegevens over het geselecteerde Album.
     *               Ook kan de gebruiker via deze klasse doorgaan naar het scherm over Artiestinfo.
     *               
     */
    public partial class AlbumInfo : Form
    {
        private int albumID;

        public AlbumInfo(int albumID)
        {
            this.albumID = albumID;
            InitializeComponent();
            btnMoreInfo.Enabled = true;

            FillFields(albumID);
            FillDgv(albumID);

        }

        /// <summary>
        /// Fill the fields with album information
        /// </summary>
        /// <param name="albumID"></param>
        private void FillFields(int albumID)
        {
            try
            {
                Album album = new Album(albumID);
                string[] albumInfo = album.GetAlbum(albumID);
                if (albumInfo != null)
                {
                    lblTitle.Text = albumInfo[1];
                    lblArtist.Text = albumInfo[5];
                    if (albumInfo[5] == "Various Artist") btnMoreInfo.Enabled = false;
                    lblPrice.Text = String.Format("{0:C}", Convert.ToDouble(albumInfo[3]));
                    lblDate.Text = albumInfo[2];
                    lblLength.Text = albumInfo[6];
                    if (albumInfo[4] != "")
                    {
                        try
                        {
                            using (System.IO.FileStream fs = new System.IO.FileStream(@".\Covers\" + albumInfo[4], System.IO.FileMode.Open, System.IO.FileAccess.Read))
                            {
                                pbCover.Image = Image.FromStream(fs);
                            }
                        }
                        catch (System.IO.FileNotFoundException ex)
                        {
                            MessageBox.Show("The following file was not found: \r\n\r\n" + ex.FileName);
                            album.AddCover("", albumID);
                        }
                    }
                }
            }
            catch (ArgumentOutOfRangeException ex)
            {
                MessageBox.Show("Album ID was not found. \r\n\r\nThis is the Error message: \r\n" + ex.Message, "ÏD not found", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        /// <summary>
        /// Fill the DGV
        /// </summary>
        /// <param name="albumID"></param>
        private void FillDgv(int albumID)
        {
            SQLServices sqlService = new SQLServices();

            string command = "SELECT songrelease.song_alb_track Nr, song.song_name Title, artist.art_name Artist, genre.genre_name Genre, CONVERT(nvarchar, song.song_length/60) + ':' + CASE WHEN song.song_length%60<10 THEN '0' + CONVERT(nvarchar, song.song_length%60) ELSE CONVERT(nvarchar, song.song_length%60) END Lengte, song.song_date Jaar " +
                "FROM Song LEFT OUTER JOIN genre ON genre.genre_id = song.song_genre, songrelease, artist " +
                "WHERE songrelease.song_id = song.song_id AND song.art_id = artist.art_id AND songrelease.alb_id = " + albumID + " " +
                "ORDER BY songrelease.song_alb_track";

            List<string[]> content = sqlService.GetTable(command);

            for (int i = 0; i < content[0].Length; i++) // Fill the DGV
            {
                dgvSongs.Columns.Add(content[0][i], content[0][i]);
            }
            for (int i = 1; i < content.Count; i++)
            {
                dgvSongs.Rows.Add(content[i]);
            }

            if (lblArtist.Text != "Various Artists")
            {
                dgvSongs.Columns[2].Visible = false;
                btnMoreInfo.Visible = true;
            }

            dgvSongs.Columns[0].Width = 30;
            dgvSongs.Columns[1].Width = 175;
            dgvSongs.Columns[2].Width = 150;
            dgvSongs.Columns[3].Width = 85;
            dgvSongs.Columns[4].Width = 55;
            dgvSongs.Columns[5].Width = 55;
        }

        private void btnMoreInfo_Click(object sender, EventArgs e)
        {
            string artistName = lblArtist.Text;
            int artistID = 0;

            SQLServices sqlService = new SQLServices();
            string command = "SELECT alb_artist FROM album WHERE alb_id = " + albumID;
            List<string[]> table = sqlService.GetTable(command);
            if (table.Count == 2)
            {
                artistID = Convert.ToInt32(table[1][0]);
            }

            if (artistID != 0)
            {
                ArtistInfo artistinfo = new ArtistInfo(artistID, artistName);
                artistinfo.Show();
            }
        }
    }
}
